//
//  NEBaseTeamHistoryFileMessageController.swift
//  NETeamUIKit
//
//  Created by Laura on 2024/7/30.
//

import NIMSDK
import UIKit

@objcMembers
open class TeamHistoryImageMessageController: NEBaseTeamHistoryImageMessageController {
    override public init(teamId: String?) {
      super.init(teamId: teamId)
      tag = "TeamHistoryImageMessageController"
    }

    public required init?(coder: NSCoder) {
        super.init(coder: coder)
    }

    override open func setupSubviews() {
        super.setupSubviews()
        view.backgroundColor = .normalTeamBackColor

        NSLayoutConstraint.activate([
            collectionView.topAnchor.constraint(equalTo: view.topAnchor, constant: NEConstant.navigationAndStatusHeight),
            collectionView.leftAnchor.constraint(equalTo: view.leftAnchor, constant: 14),
            collectionView.rightAnchor.constraint(equalTo: view.rightAnchor, constant: -14),
            collectionView.bottomAnchor.constraint(equalTo: view.bottomAnchor),
        ])
      
        collectionView.register(TeamSearchImageCell.self, forCellWithReuseIdentifier: "\(TeamSearchImageCell.self)")
      
        collectionView.register(TeamSearchDateHeader.self, forSupplementaryViewOfKind: UICollectionView.elementKindSectionHeader, withReuseIdentifier: "\(TeamSearchDateHeader.self)")
    }
    

    override open func collectionView(_ collectionView: UICollectionView, viewForSupplementaryElementOfKind kind: String, at indexPath: IndexPath) -> UICollectionReusableView {
                
        if let header = collectionView.dequeueReusableSupplementaryView(ofKind: kind, withReuseIdentifier: "\(TeamSearchDateHeader.self)", for: indexPath) as? TeamSearchDateHeader {
            header.titleLabel.text = viewModel.searchImageResultInfoKeys[indexPath.section]
            return header
        }

        return TeamSearchDateHeader()
    }
    
    override open func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        if let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "\(TeamSearchImageCell.self)", for: indexPath) as? TeamSearchImageCell {
            let messages = viewModel.searchImageResultInfos[viewModel.searchImageResultInfoKeys[indexPath.section]] ?? []
                        
            let rawAttachContent = messages[indexPath.row].rawAttachContent ?? ""
            
            print(rawAttachContent)
            if let messageContent = jsonStringToDictionary(jsonString: rawAttachContent), let url = messageContent["url"] as? String {
                
                if messages[indexPath.row].messageType == NIMMessageType.video  {
                    let thumbUrl = V2NIMStorageUtil.videoCoverUrl(url, offset: 0)
                    cell.imageView.sd_setImage(with: URL(string: thumbUrl))
                    cell.collectionStateView.isHidden = false
                } else {
                    cell.imageView.sd_setImage(with: URL(string: url))
                    cell.collectionStateView.isHidden = true
                }
                
            }
            return cell
        }
        
        return UICollectionViewCell()
    }
}
